package com.tsfot.spider.analyze;

import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tsfot.spider.bean.SpiderData;
import com.tsfot.spider.network.NetworkService;
import com.tsfot.spider.service.PersistenceService;
import com.tsfot.spider.task.Task;


/**
 * 单页解析服务
 * @author frank0
 *
 */
public class SinglePageAnalyService extends AbstractAnalyzeService{

	private final static Logger logger=LoggerFactory.getLogger(SinglePageAnalyService.class);
	
	private PersistenceService persistenceService;
	
	private NetworkService networkService;
	
	private LinkedBlockingQueue<Task> singlePageQueue = null;
	
	private final static int DEFAULT_BATCH_SIZE=50;
	
	private static List<SpiderData> dataList =Lists.newArrayList();
	
	protected SpiderData doAnalysis(Task task) {
		String filePath=task.getLocalPath();
		//读取 进行分析
		String content = fileService.readFileToString(filePath);
		Document doc = Jsoup.parse(content);
		//根据配置的字段进行解析
		Map<String, String> dataKeyMap = config.getDataKeyMap();
		
		SpiderData data=new SpiderData();
		Map<String,String> result=Maps.newHashMap();
		boolean dataActive=false;
		for (String key: dataKeyMap.keySet()) {
			String value = analysisKey(doc,dataKeyMap.get(key));
			result.put(key,value);
			if(!StringUtils.isEmpty(value)){
				dataActive=true;
			}
			if(key.contains("cover")||key.contains("img")){
				String url = result.get(key);
				/*//需要下载图片
				logger.info("文件名称：{},找到图片链接:{}",filePath,url);
				if(url!=null&&!url.equals("")){
					String suffix = url.substring(url.lastIndexOf(".") + 1);
					String imagePath=networkService.downloadImage(url,task.getFileName()+"_img."+suffix);
					result.put(key, imagePath);
				}*/
			}
		}
		if(dataActive){
			data.setResult(result);
		}
		return data;
	}
	
	protected String analysisKey(Document doc,String cssQuery) {
		
		Elements select = doc.select(cssQuery);
		if(select.size()==0)return "";
		Element ele =select.get(0);
		String content = "";
		if (ele != null) {
			content = ele.html();
			if(cssQuery.contains("cover")||cssQuery.contains("img")){
				content=ele.attr("src");
			}
		}
		return content;
	}

	public void start() {
		//根据本地存储的位置，加载到内存，解析成为详情页，然后下载，生成详情页任务，放入队列
		logger.info("启动详细页分析服务...");
		try {
			while(true){
				Task listPageTask = singlePageQueue.take();
				if(listPageTask==Task.NO_TASK){
					break;
				}
				//根据配置生存实体，插入list等待入库
				SpiderData data=doAnalysis(listPageTask);
				dataList.add(data);
				//检查是否达到设置阈值
				if(dataList.size()>=DEFAULT_BATCH_SIZE){
					persistenceService.saveSpiderData(dataList);
					dataList.clear();
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	public void stop() {
		// TODO Auto-generated method stub
		
	}

	public void setSinglePageQueue(LinkedBlockingQueue<Task> singlePageQueue) {
		this.singlePageQueue = singlePageQueue;
	}

	public PersistenceService getPersistenceService() {
		return persistenceService;
	}

	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	public NetworkService getNetworkService() {
		return networkService;
	}

	public void setNetworkService(NetworkService networkService) {
		this.networkService = networkService;
	}
}
